package xworker.netty.handlers.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpResponseStatus;

public class HttpHandler {
    public static final String JSON = "json";
    public static final String FREEMARKER = "freemarker";
    public static final String REDIRECT = "redirect";

    private static final ThreadLocal<RequestBean> requestBeanThreadLocal = new ThreadLocal<>();

    private static final ThreadLocal<HttpResponseStatus> statusThreadLocal = new ThreadLocal<>();

    private static final ThreadLocal<ChannelHandlerContext> ctxThreadLocal = new ThreadLocal<>();

    private static final ThreadLocal<String> contentTypeThreadLocal = new ThreadLocal<>();

    private static final ThreadLocal<String> resultHandlerThreadLocal = new ThreadLocal<>();
    private static final ThreadLocal<Boolean> handledThreadLocal = new ThreadLocal<>();

    protected static Boolean isHandled(){
        Boolean handled = handledThreadLocal.get();
        return handled != null ? handled : false;
    }

    protected static void setHandled(boolean handled){
        handledThreadLocal.set(handled);
    }

    public static String getResultHandler(){
        return resultHandlerThreadLocal.get();
    }

    public static void setResultHandler(String handler){
        resultHandlerThreadLocal.set(handler);
    }

    public static String getResponseContentType(){
        return contentTypeThreadLocal.get();
    }

    public static void setResponseContentType(String contentType){
        contentTypeThreadLocal.set(contentType);
    }

    public static ChannelHandlerContext getChannelHandlerContext(){
        return ctxThreadLocal.get();
    }

    public static void setChannelHandlerContext(ChannelHandlerContext ctx){
        ctxThreadLocal.set(ctx);
    }

    public static HttpResponseStatus getHttpResponseStatus(){
        return statusThreadLocal.get();
    }

    public static void setHttpResponseStatus(HttpResponseStatus status){
        statusThreadLocal.set(status);
    }

    public static RequestBean getRequestBean(){
        return requestBeanThreadLocal.get();
    }

    public static void setRequestBean(RequestBean requestBean){
        requestBeanThreadLocal.set(requestBean);
    }

    protected static void clear(){
        requestBeanThreadLocal.set(null);
        statusThreadLocal.set(null);
        ctxThreadLocal.set(null);
        contentTypeThreadLocal.set(null);
        resultHandlerThreadLocal.set(null);
    }

    protected static void initRequst(RequestBean requestBean){
        requestBeanThreadLocal.set(requestBean);
    }
}
